package game;
//创建的类


import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.*;




//从java类库倒的类包
public class indexjf extends JFrame implements KeyListener{//创建主页面的类
    int[][] date = new int[4][4];
    int x;
    int y;
    String path = "image\\animal\\animal1\\";
    long ch = new Date().getTime();
    long cha;
    //类中所用的成员变量





    public indexjf(){//构造方法
        margin();//主界面添加的一系列效果


        menu();//添加菜单栏


        imagesmax();//将添加上的图片打乱顺序


        win();    //游戏胜利后进行的标记函数


        images();//将图片加入到JFrame界面中的方法


        key();//键盘监听事件


        this.setVisible(true);//创建窗口可见
    }







    //将图片加入到JFrame界面中的方法
    public void images() {

        this.getContentPane().removeAll();
        if (win()){
            JLabel win = new JLabel(new ImageIcon("image\\win.png"));
            win.setBounds(230,283,197,73);
            this.getContentPane().add(win);
        }
        Date d2 = new Date();
        cha = d2.getTime();
        JLabel dat = new JLabel("时间:" + ((cha-ch)/1000) + "秒");
        dat.setFont(new Font("宋体", 1, 15));
        dat.setBounds(50, 30, 100, 20);
        this.getContentPane().add(dat);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (date[i][j] == 0)
                {
                    x = i;
                    y = j;
                    continue;
                }

                int num = date[i][j];
                JLabel jl = new JLabel(new ImageIcon(path + num +".jpg"));
                jl.setLayout(null);
                jl.setBorder(new BevelBorder(1));
                jl.setBounds(105 * j + 83,105 * i + 134,105,105);
                this.getContentPane().add(jl);
            }
        }
        ImageIcon bg = new ImageIcon("image\\background.png");
        JLabel bgc = new JLabel(bg);
        bgc.setBounds(40,40,508,560);
        this.getContentPane().add(bgc);
        this.getContentPane().repaint();
    }










    //键盘监听事件
    public void key(){
        this.addKeyListener(this);
    }









    //将添加上的图片打乱顺序
    public void imagesmax(){
        int[] imageArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        for (int i = 0; i < imageArr.length; i++) {
            Random r = new Random();
            int temp = r.nextInt(imageArr.length);
            int t = imageArr[i];
            imageArr[i] = imageArr[temp];
            imageArr[temp] = t;
        }
        for (int i = 0; i < imageArr.length; i++) {
            date[i/4][i%4] = imageArr[i];
        }

    }









    //添加菜单栏
    public void menu() {
        JMenuBar bar = new JMenuBar();
        JMenu function = new JMenu("主要功能");
        JMenu about = new JMenu("关于我们");
        JMenu one = new JMenu("更换图片");
        JMenuItem two = new JMenuItem("重新开始");
        JMenuItem three = new JMenuItem("重新登陆");
        JMenuItem four = new JMenuItem("退出游戏");

        JMenuItem five = new JMenuItem("联系方式");
        JMenuItem six = new JMenuItem("游戏规则");
        function.setFont(new Font("宋体", 1, 15));
        about.setFont(new Font("宋体", 1, 15));
        one.setFont(new Font("宋体", 1, 15));
        two.setFont(new Font("宋体", 1, 15));
        three.setFont(new Font("宋体", 1, 15));
        four.setFont(new Font("宋体", 1, 15));
        five.setFont(new Font("宋体", 1, 15));
        six.setFont(new Font("宋体", 1, 15));
        bar.add(function);
        bar.add(about);
        function.add(one);
        function.add(two);
        function.add(three);
        function.add(four);
        about.add(five);
        about.add(six);
        this.setJMenuBar(bar);



        //菜单栏的按钮监听事件
        two.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ch = new Date().getTime();
                imagesmax();
                images();
            }
        });
        four.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);//关闭虚拟机
            }
        });
        six.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                indexjf.super.getContentPane().removeAll();
                JLabel jl = new JLabel("<html>本游戏是一款简单的拼图小游戏，当您长按a是可显示原图的，在菜单栏里有改变图片的按钮和重新开始的按钮，规则很简单，祝您游戏愉快!</html>");
                jl.setBounds(103,154,380,150);
                jl.setFont(new Font("宋体", 0, 25));
                indexjf.super.getContentPane().add(jl);
                JLabel jl1 = new JLabel("--zzuli高代起");
                jl1.setBounds(320,480,200,50);
                jl1.setFont(new Font("宋体", 0, 25));
                indexjf.super.getContentPane().add(jl1);
                ImageIcon bg = new ImageIcon("image\\background.png");
                JLabel bgc = new JLabel(bg);
                bgc.setBounds(40,40,508,560);
                indexjf.super.getContentPane().add(bgc);
                indexjf.super.getContentPane().repaint();
            }
        });
        five.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog jDialog = new JDialog();
                JLabel jLabel = new JLabel(new ImageIcon("image\\erweima.png"));
                jLabel.setBounds(0,0,258,300);
                jDialog.getContentPane().add(jLabel);
                jDialog.setSize(400,400);
                jDialog.setLocationRelativeTo(null);
                jDialog.setAlwaysOnTop(true);
                jDialog.setVisible(true);
                jDialog.setModal(true);//弹窗不关闭则无法实行其他程序
            }
        });






        JMenuItem one1 = new JMenuItem("Animals");
        JMenuItem two2 = new JMenuItem("girls");
        JMenuItem three3 = new JMenuItem("sport");
        one.add(one1);
        one.add(two2);
        one.add(three3);






        one1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random random = new Random();
                int a = random.nextInt(8);
                path = "image\\animal\\animal"+(a+1)+"\\";
                ch = new Date().getTime();
                imagesmax();
                images();
            }
        });
        two2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random random = new Random();
                int a = random.nextInt(11);
                path = "image\\girl\\girl"+(a+1)+"\\";
                ch = new Date().getTime();
                imagesmax();
                images();
            }
        });
        three3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random random = new Random();
                int a = random.nextInt(10);
                path = "image\\sport\\sport"+(a+1)+"\\";
                ch = new Date().getTime();
                imagesmax();
                images();
            }
        });
    }











    //主界面添加的一系列特效
    public void margin() {
        this.setSize(603, 680);//主界面的宽和高
        this.setTitle("拼图小游戏");//标题
        this.setAlwaysOnTop(true);//一直最上方
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(3);//设置程序的关闭
        this.setLayout(null);//关闭隐藏容器的默认设置
    }










    //键盘监听事件
    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == 65){
            this.getContentPane().removeAll();
            JLabel jl = new JLabel(new ImageIcon(path + "all.jpg"));
            jl.setBounds(83, 134, 420, 420);
            this.getContentPane().add(jl);
            JLabel jl1 = new JLabel(new ImageIcon("image\\background.png"));
            jl1.setBounds(40,40,508,560);
            this.getContentPane().add(jl1);
            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //左：37， 上：38， 右：39， 下：40
        int temp;
        if (e.getKeyCode()==39&&y-1>=0){
            date[x][y] = date[x][y-1];
            date[x][y-1] = 0;
            y--;
            images();
        }
        if (e.getKeyCode()==40&&x-1>=0){
            date[x][y] = date[x-1][y];
            date[x-1][y] = 0;
            x--;
            images();
        }
        if (e.getKeyCode()==37&&y+1<4){
            date[x][y] = date[x][y+1];
            date[x][y+1] = 0;
            y++;
            images();
        }
        if (e.getKeyCode()==38&&x+1<4){
            date[x][y] = date[x+1][y];
            date[x+1][y] = 0;
            x++;
            images();
        }
        if (e.getKeyCode()==65){
            images();
        }
        if (e.getKeyCode()==87){
             date = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};
             images();
        }
    }









    //游戏胜利后进行的标记函数
    public boolean win(){
        int[][] win= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};
        int flag = 1;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if(date[i][j] != win[i][j]){
                    flag = 0;
                    break;
                }
            }
        }
        if (flag == 1){
            return true;
        }
        else
            return false;
    }
}
